* Table A1b: Summary statistics by insurance purchase status
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* using balanced_panel
keep if bal_panel3==1

* control farmers are not offered insurance, drop
drop if vtgroup=="Control"

bys id: egen bought_all=max(bought)

local vars "kval_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 profit_k_w1_real fraction_hy_k fraction_cashcrop_k plotsize_ha totalrevenues_w1_real totalcosts_w1_real totalprofit_w1_real"

* use lag here because marketing was done after surveying for that year
sort id wave
foreach x of local vars {
bys id: gen lag_`x'=`x'[_n-1]
}

local vars1 "lag_kval_w1_real lag_cost_k_w1_real lag_b110rk_w1_real lag_b111rk_w1_real lag_b112pk_w1_real lag_total_mandays_k_w1 lag_total_hired_mandays_k_w1 lag_total_fam_mandays_k_w1 lag_profit_k_w1_real lag_fraction_hy_k lag_fraction_cashcrop_k lag_plotsize_ha lag_totalrevenues_w1_real lag_totalcosts_w1_real lag_totalprofit_w1_real"

* baseline outcome var, ever bought insurance
qui estpost summarize `vars' if bought_all==1 & mkt_year==2005 
est sto A 
qui estpost summarize `vars' if bought_all==0 & mkt_year==2005 
est sto B
* contemporaneous year, lag outcome var
qui estpost summarize `vars1' if bought==1
est sto C
qui estpost summarize `vars1' if bought==0 
est sto D 
esttab A B C D using "../output/sum-a1b.csv", cells("mean(fmt(2))" replace sd(par fmt(2))) nolabel noabbrev title("Summary statistics") collabels("Mean") nonum append

* balance tests
foreach var of local vars {
reg `var' bought_all if mkt_year == 2005, cluster(villageno)
est sto A
test bought_all
estadd scalar pval = r(p)
esttab A using "../output/bal-1b.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
}
estimates clear

foreach var of local vars1 {
reg `var' bought , cluster(villageno)
est sto B
test bought
estadd scalar pval = r(p)
esttab B using "../output/bal-1b.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
}
